package com.twinkle.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twinkle.dao.ClassifierResultMapper;
import com.twinkle.model.ClassifierResult;
import com.twinkle.service.ClassifierResultService;
import com.twinkle.utils.JqgridUtil;
import com.twinkle.utils.ParamTranslate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * @title: ClassifierResultServiceImpl
 * @description:
 * @author: Paul
 * @date: 2023/3/20 11:41
 */
@Service
public class ClassifierResultServiceImpl implements ClassifierResultService {

    @Autowired
    ClassifierResultMapper classifierResultMapper;
    @Autowired
    private JqgridUtil jqgridUtil;

    @Override
    public ClassifierResult getClassifierResultByModelId(String modelId) {
        ClassifierResult classifierResult = classifierResultMapper.selectOne(new QueryWrapper<ClassifierResult>().eq("model_id", modelId));
        //格式化param
        StringBuffer paramFormat = new StringBuffer();
        String param = classifierResult.getParam();
        JSONObject paramObject = JSONObject.parseObject(param);
        for (String key : paramObject.keySet()) {
            paramFormat.append(ParamTranslate.getParamName(key) + " : " + paramObject.getString(key) + "\r\n");
        }
        classifierResult.setParam(paramFormat.toString());

        String predDisImage1 = classifierResult.getPredDisImage();
        if (StringUtils.isNotBlank(predDisImage1)) {
            if (predDisImage1.endsWith("png") || predDisImage1.endsWith("pdf")) {
                String[] splitTwoDimDisImage = predDisImage1.split("\\/");
                predDisImage1=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
                classifierResult.setPredDisImage(predDisImage1);
            } else {
                classifierResult.setPredDisImage(null);
            }

        }
        return classifierResult;
    }

    @Override
    public ClassifierResult getClassifierResultById(String id) {
        ClassifierResult classifierResult = classifierResultMapper.selectById(id);
        //格式化param
        StringBuffer paramFormat = new StringBuffer();
        String param = classifierResult.getParam();
        JSONObject paramObject = JSONObject.parseObject(param);
        for (String key : paramObject.keySet()) {
            paramFormat.append(ParamTranslate.getParamName(key) + " : " + paramObject.getString(key) + "\r\n");
        }
        classifierResult.setParam(paramFormat.toString());

        String predDisImage1 = classifierResult.getPredDisImage();
        if (StringUtils.isNotBlank(predDisImage1)) {
            if (predDisImage1.endsWith("png") || predDisImage1.endsWith("pdf")) {
                String[] splitTwoDimDisImage = predDisImage1.split("\\/");
                predDisImage1=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
                classifierResult.setPredDisImage(predDisImage1);
            } else {
                classifierResult.setPredDisImage(null);
            }
        }
        return classifierResult;
    }

    @Override
    public String getAllClassifierResultJson(int page, int count) {
        List<ClassifierResult> all = classifierResultMapper.selectList(null);
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        List<ClassifierResult> list = all.subList(count * (page - 1), toIndex);
        List<ClassifierResult> listNew = new ArrayList<>();
        for (ClassifierResult classifierResult : list) {
            String predDisImage1 = classifierResult.getPredDisImage();
            if (StringUtils.isNotBlank(predDisImage1)) {
                String[] splitTwoDimDisImage = predDisImage1.split("\\/");
                predDisImage1=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
                classifierResult.setPredDisImage(predDisImage1);
            }
            listNew.add(classifierResult);
        }
        return jqgridUtil.getJson(listNew, page + "", all.size() , count);
    }

    @Override
    public String classifierResultListForUserAdd() {
        List<ClassifierResult> list = classifierResultMapper.selectList(null);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).getId() + ":" + list.get(i).getAlgoName() + ";");
        }
        return sb.substring(0, sb.length()-1);
    }

    @Override
    public String addClassifierResult(ClassifierResult classifierResult) throws ParseException {
        int result = classifierResultMapper.insert(classifierResult);
        return result+"";
    }

    @Override
    public String modifyClassifierResult(ClassifierResult classifierResult) {
        int result = classifierResultMapper.updateById(classifierResult);
        return result+"";
    }

    @Override
    public String deleteClassifierResult(String id) {
        int result = classifierResultMapper.deleteById(Integer.parseInt(id));
        return result+"";
    }

    @Override
    public List<ClassifierResult> getAllClassifierResult() {
        List<ClassifierResult> all = classifierResultMapper.selectList(null);
        return all;
    }

    @Override
    public String getAllClassifierResultJsonBySearch(int page, int count, String algoName, String appName) {
        QueryWrapper<ClassifierResult> classifierResultQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(algoName)) {
            classifierResultQueryWrapper.eq("algo_name", algoName);
        }
        if (StringUtils.isNotBlank(appName)) {
            classifierResultQueryWrapper.eq("app_name", appName);
        }
        List<ClassifierResult> all = classifierResultMapper.selectList(classifierResultQueryWrapper);
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        List<ClassifierResult> list = all.subList(count * (page - 1), toIndex);
        List<ClassifierResult> listNew = new ArrayList<>();
        for (ClassifierResult classifierResult : list) {
            String predDisImage1 = classifierResult.getPredDisImage();
            if (StringUtils.isNotBlank(predDisImage1)) {
                String[] splitTwoDimDisImage = predDisImage1.split("\\/");
                predDisImage1=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
                classifierResult.setPredDisImage(predDisImage1);
            }
            listNew.add(classifierResult);
        }
        return jqgridUtil.getJson(listNew, page + "", all.size() , count);
    }

}
